#include <bits/stdc++.h>
using namespace std;

bool check(string s) {
    string t = s;
    reverse(t.begin(), t.end());
    return s == t;
}

void solve() {
    string s; cin >> s;
    int x; cin >> x;
    int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3));
    map<string, int> mp;
    int ans = 0;
    if(check(s)) ans++;
    mp[s]++;
    while(1) {
        t += x;
        t %= 1440;
        string tem;
        int p2 = t % 60;
        int p1 = t / 60;
        if(p1 < 10) {
            tem += '0';
            tem += (p1 + '0');
        }else{
            tem += to_string(p1);
        }
        tem += ':';
        if(p2 < 10) {
            tem += '0';
            tem += (p2 + '0');
        }else{
            tem += to_string(p2);
        }
        if(mp.count(tem)) break;
        if(check(tem)) ans++;
        mp[tem]++;
    }
    cout << ans << endl;
}

signed main() {
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}